<template>
	<basic-container>
		<avue-crud
			:option="option"
			:data="data"
			ref="crud"
			v-model="form"
			:table-loading="loading"
			:permission="permissionList"
			:page="page"
			@refresh-change="refreshChange"
			@search-change="searchChange"
			@search-reset="searchReset"
			@current-change="currentChange"
			@size-change="sizeChange"
			@on-load="onLoad"
		>
			<!-- 自定义添加搜索条件  按日期搜索  -->
			<template slot="search">
				<el-col :md="12" :xs="12" style="height: 52px;">
					<el-form-item label="有效期">
						<el-date-picker
							text="有效期"
							v-model="acTime"
							type="datetimerange"
							range-separator="至"
							size="mini"
							start-placeholder="活动开始日期"
							:default-time="['12:00:00', '22:00:00']"
							end-placeholder="活动结束日期"
							align="right"
							format="yyyy-MM-dd HH:mm:ss"
							value-format="yyyy-MM-dd HH:mm:ss"
						></el-date-picker>
					</el-form-item>
				</el-col>
			</template>
			<!-- <template slot-scope="scope" slot="statistics">
				<el-button @click="statisticsBut(scope.row)" size="mini" type="text">统计信息</el-button>
			</template>
			<template slot-scope="scope" slot="activityLink">
				<el-button @click="activityLink(scope.row)" size="mini" type="text">活动链接</el-button>
			</template> -->
			<template slot-scope="" slot="menuLeft">
				<el-button @click="addNew()" icon="el-icon-plus" size="mini" type="primary">新增活动</el-button>
			</template>
			<template slot-scope="scope" slot="menu">
				<el-button v-if="scope.row.acStage == 1 && scope.row.acStatus==1" @click="enterActivity(scope.row)" size="mini" type="text">参加活动</el-button>
				<el-button @click="applyList(scope.row.id)" size="mini" type="text">活动商品</el-button>
				<el-button @click="editAct(scope.row)" v-if="(scope.row.acStage==1 || scope.row.acStage== -1 || scope.row.acStage== -2 ) || (scope.row.acStatus==0 || scope.row.acStatus==3 )" size="mini" type="text">编辑</el-button>
				<el-button @click="showAct(scope.row)"  size="mini" type="text">查看</el-button>
				<!-- <el-button @click="activityLink(scope.row)" size="mini" type="text">活动链接</el-button> -->
<!--				<el-button @click="statisticsBut(scope.row)"  size="mini" type="text">统计信息</el-button>-->
			</template>
		</avue-crud>
		<!-- 新增和编辑-->
		<el-drawer :wrapperClosable="false" :visible.sync="drawerNew" :with-header="false" size="60%">
			<div class="demo-drawer__header">
				<div class="drawerHeader">
					<strong>{{ deawerNewTitle }}</strong>
				</div>
			</div>
			<div class="demo-drawer__content">
				<avue-form ref="form" v-model="obj" :option="newOption" @submit="submit">
					<!-- <template slot-scope="scope" slot="preheatTime">
						<div class="upload">
							<div class="upload">
								<el-date-picker
									v-model="obj.preheatTime"
									format="yyyy-MM-dd HH:mm:ss"
									value-format="yyyy-MM-dd HH:mm:ss"
									type="datetime"
									placeholder="选择活动预热时间"
									align="right"
									:picker-options="pickerOption"
								></el-date-picker>
							</div>
						</div>
					</template> -->
					<template slot-scope="scope" slot="acApplyEndTime">
						<div class="upload">
							<el-date-picker
								:disabled="isView"
								v-model="obj.acApplyEndTime"
								format="yyyy-MM-dd HH:mm:ss"
								value-format="yyyy-MM-dd HH:mm:ss"
								type="datetime"
								placeholder="选择报名截止日期时间"
								align="right"
								:picker-options="pickerOption"
							></el-date-picker>
						</div>
					</template>
					<template slot-scope="scope" slot="acTime">
						<div class="upload">
							<el-date-picker v-model="obj.acTime" :disabled="isView" type="datetimerange" :picker-options="pickerOption" range-separator="至"
											start-placeholder="活动开始日期" end-placeholder="活动结束日期"  align="right"
											format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"
											>
							</el-date-picker>

						</div>
					</template>
					<template slot-scope="scope" slot="acPcAccUrl">
						<div class="upload">
							<el-input style="margin-right: 0.2rem;" :disabled="isView" @focus="uploadbtn(1)" v-model="obj.acPcAccUrl"   :clearable="true" placeholder="最佳尺寸:1280px*500px"/>
							<el-image
								v-if="obj.acPcAccUrl"
								@click="uploadbtn"
								fit="contain"
								:src="obj.acPcAccUrl"
								style="margin-right: 0.2rem;width: 50px; height: 50px"
							></el-image>
							<el-button size="mini" type="primary" @click="uploadbtn(1)">上传图片</el-button>
							<p class="tsmsg">最多可上传1张照片</p>
						</div>
					</template>
					<template slot-scope="scope" slot="acH5AccUrl">
						<div class="upload">
							<el-input style="margin-right: 0.2rem;" :disabled="isView" @focus="uploadbtn(2)"   v-model="obj.acH5AccUrl" placeholder="最佳比例2:1"/>
							<el-image
								v-if="obj.acH5AccUrl"
								@click="uploadbtn"
								fit="contain"
								:src="obj.acH5AccUrl"
								style="margin-right: 0.2rem;width: 50px; height: 50px;margin: 0 0.2rem;"
							></el-image>
							<el-button size="mini" type="primary" @click="uploadbtn(2)">上传图片</el-button>
							<p class="tsmsg">最多可上传1张照片</p>
						</div>
					</template>
					<template slot-scope="scope" slot="appPcAccUrl">
						<div class="upload">
							<el-input style="margin-right: 0.2rem;" :disabled="isView" v-model="obj.appPcAccUrl" placeholder="图片比例：1：1"  :readonly="true" />
							<el-image
								v-if="obj.appPcAccUrl"
								@click="uploadbtn"
								fit="contain"
								:src="obj.appPcAccUrl"
								style="margin-right: 0.2rem;margin: 0 0.2rem;width: 50px; height: 50px"
							></el-image>
							<el-button size="mini" type="primary" @click="uploadbtn(3)">上传图片</el-button>
							<p class="tsmsg">最多可上传1张照片</p>
						</div>
					</template>
					<template slot-scope="scope" slot="menuForm">
						<el-button v-if="!isView" @click="closeDrawer">取消</el-button>
						<el-button v-else @click="closeDrawer">关闭</el-button>
					</template>
				</avue-form>
			</div>
		</el-drawer>
		<!-- 参与活动 -->
		<el-drawer :wrapperClosable="false" :visible.sync="drawerActivity" :with-header="false" size="70%">
			<div class="demo-drawer__header">
				<div class="drawerHeader"><strong>参加活动</strong></div>
			</div>
			<div class="demo-drawer__content">
				<avue-form ref="formGoods" v-model="objActivity" :option="optionActivity">
					<template slot-scope="scope" slot="acContent">
						<el-input disabled :value="objActivity.acContent || '无活动说明'"></el-input>
					</template>
					<template slot="goodsStock">
						<avue-crud ref="goodsStockCrud" :data="goodsStockList" :option="objActivity.acType=='2' && objActivity.groupBuyType=='2' && storeType=='1'?goodsStock:goodsStockSelf" v-model="objGoodsStock">

							<template slot-scope="{ row }" slot="groupPrice">
								<el-input v-model="row.groupPrice" placeholder="请输入内容"></el-input>
							</template>
							<template slot-scope="{ row }" slot="stocks">
								<el-input v-model="row.stocks" placeholder="请输入内容"></el-input>
							</template>
							<template slot-scope="{ row }" slot="maxCount">
								<el-input v-model="row.maxCount" placeholder="请输入内容"></el-input>
							</template>
							<template slot-scope="{ row }" slot="maxTimes">
								<el-input v-model="row.maxTimes" placeholder="请输入内容"></el-input>
							</template>
							<!--							当活动类型是 2（拼团），group_buy_type 为 2（团长分销），非自营店铺 1，才显示团长分销-->
							<template slot-scope="{ row }" slot="leaderReward">
								<el-input type="number" :min="0" v-model="row.leaderReward" placeholder="请输入内容"></el-input>
							</template>
						</avue-crud>
					</template>
					<template slot="goods">
						<avue-crud
							ref="goodsCrud"
							:data="goodsList"
							:option="optionGoods"
							v-model="objGoods"
							:table-loading="goodsloading"
							:permission="permissionList"
							:page="goodsPage"
							@selection-change="selectionChange"
							@refresh-change="refreshGoodsChange"
							@search-change="searchGoodsChange"
							@search-reset="searchGoodsReset"
							@current-change="currentGoodsChange"
							@size-change="sizeGoodsChange"
						>
							<template slot="tip">
								<span style="color: #999;"> &nbsp; &nbsp;  (禁选说明：已参加活动商品禁选)</span>
							</template>
							<template slot-scope="{ row }" slot="goodsMainPhotoUrl">
								<img :src="row.goodsMainPhotoUrl" style="width:50px;height:50px;" />
							</template>
							<template slot="search">
								<el-col :span="10"  style="height: 52px;">
									<el-form-item label="商品分类" label-width="100px"  style="display: inline-block;width: 80%;">
										<el-cascader v-model="goodsClassLabel" :options="chooseProductUserOption" size="mini" @change="handleUserChange"></el-cascader>
									</el-form-item>
								</el-col>
							</template>
						</avue-crud>
					</template>
					<template slot="menuForm">
						<el-button type="primary" @click="handleSubmit" :loading="load">提 交</el-button>
						<el-button @click="handleEmpty">关闭</el-button>
					</template>
				</avue-form>
			</div>
		</el-drawer>
		<!-- 查看活动商品 -->
		<el-drawer :visible.sync="drawerList" :with-header="false" size="60%" :show-close="true">
			<div class="demo-drawer__header">
				<div class="drawerHeader"><strong>活动商品</strong></div>
			</div>
			<div class="demo-drawer__content" style="padding: 30px;">
				<avue-crud
					:data="dataList"
					:option="optionList"
					v-model="objList"
					ref="actGoodsList"
					:table-loading="actGoodsLoading"
					:permission="permissionList"
					:page="actGoodsPage"
					@refresh-change="refreshActGoodsChange"
					@search-change="searchActGoodsChange"
					@search-reset="searchActGoodsReset"
					@current-change="currentActGoodsChange"
					@size-change="sizeActGoodsChange"
				>
					<template slot-scope="scope" slot="menu">
						<el-button @click="goodsDetail(scope.row.goodsId)" size="mini" type="text">详情</el-button>
					</template>
				</avue-crud>
			</div>
		</el-drawer>
		<!-- 查看活动路径-->
		<el-dialog title="活动路径" :visible.sync="dialogLink" width="30%">
			<div class="diaLink">
				<!-- <strong style="color: #000;">PC端活动路径：</strong>
				<p>{{ pcLink }}</p> -->
				<strong style="color: #000;">H5端活动路径：</strong>
				<p>{{ h5Link }}</p>
			</div>
			<span slot="footer" class="dialog-footer"><el-button type="primary" @click="dialogLink = false">关 闭</el-button></span>
		</el-dialog>
		<selFile ref="selFile" selectType="radio" @callback="selFileCallBack" albumCode="store" dialogTitle="选择图片1" selMaxWidth="500px" selMaxHeight="1280px"></selFile>
		<el-drawer title="商品详情" size="70%" :withHeader="false" :visible.sync="dialogTableVisible" width="70%">
			<add-goods v-if="dialogTableVisible" :disadledSpec="disadledSpec" :goodsId="goodsId" :isPublish="isPublish" :isCheck="isCheck" :isRedact="isRedact"></add-goods>
		</el-drawer>
	</basic-container>
</template>

<script>
import selFile from '@/components/file/selFile';
import { showGoodsStock } from '@/api/base/activity/seller/spikeAct';
import { saveOrUpdate, getActList, joinActivity, listActivity,getGoodsList,getStoreInfo,getClassTree } from '@/api/base/activity/seller/discountAct';
import addGoods from '@/views/base/goods/seller/addGoods';
import { mapGetters } from 'vuex';
import { dateFormat } from '@/util/date.js';
var baseUrl='https://testapimall.sunac.com.cn/'
var Dict = JSON.parse(localStorage.getItem('mall-dictionary')).content;
import { validatNum} from '@/util/util.js';
import {validatPrice} from '@/util/validate.js' // 校验团长可得金额
var VAILD = [
	{
		label: '不包邮',
		value: 0
	},
	{
		label: '包邮',
		value: 1
	}
];
var YES_OR_NO = [{
	label: '团长分销',
	value: 2
}, {
	label: '普通拼团',
	value: 1
}, ]
var YES_OR_NO2 = [{
	label: '不显示',
	value: 0
}, {
	label: '显示',
	value: 1
}, ]
var OPEN_OR_NOT = [{
	label: '关闭',
	value: 0
}, {
	label: '开启',
	value: 1
}, ]
var YES_OR_NO_JOIN_GROUP = [{
	label: '不凑团',
	value: 2
}, {
	label: '凑团',
	value: 1
}, ]
export default {
	data() {
		return {
		goodsClassLabel:'',
			chooseProductUserOption:[], // 分类的列表
			load:false,
			goodsClassId:'',
			pickerOptions: {
				shortcuts: [
					{
						text: '未来一周',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							end.setTime(start.getTime() + 3600 * 1000 * 24 * 7);
							picker.$emit('pick', [start, end]);
						}
					},
					{
						text: '未来一个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							end.setTime(start.getTime() + 3600 * 1000 * 24 * 30);
							picker.$emit('pick', [start, end]);
						}
					},
					{
						text: '未来三个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							end.setTime(start.getTime() + 3600 * 1000 * 24 * 90);
							picker.$emit('pick', [start, end]);
						}
					}
				]
			},
			pickerOption: {
				disabledDate(time) {
					return time.getTime() + 3600 * 1000 * 24 * 1 < Date.now();
				},
				shortcuts: [
					{
						text: '今天',
						onClick(picker) {
							picker.$emit('pick', new Date());
						}
					},
					{
						text: '明天',
						onClick(picker) {
							const date = new Date();
							date.setTime(date.getTime() + 3600 * 1000 * 24);
							picker.$emit('pick', date);
						}
					},
					{
						text: '一周后',
						onClick(picker) {
							const date = new Date();
							date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
							picker.$emit('pick', date);
						}
					}
				]
			},
			form: {},
			pcLink: '',
			h5Link: '',
			spikeTimes: [],
			objActivity: {}, //参与活动对象
			query: {
				type: 1,
				acType: 2
			},
			dialogTableVisible: false, //商品详情
			isPublish: false,
			isCheck: false,
			isRedact: false,
			disadledSpec: false,
			goodsId: '',
			drawerNew: false,
			isView: false,
			dialogLink: false,
			deawerNewTitle: '新增活动',
			drawerList: false,
			drawerActivity: false,
			obj: {
				seckillEndTime: [],
				seckillStartTime: []
			},
			dataList: [],
			//参加活动商品
			goodsList: [],
			imageType: 1,
			data: [],
			loading: true,
			goodsloading: true,
			rolesTree: [],
			rolesKeys: [],
			acTime: '',
			box: false,
			page: {
				pageSize: 10,
				currentPage: 1,
				total: 0
			},
			goodsPage: {
				pageSize: 10,
				currentPage: 1,
				total: 0
			},
			idList: [],
			goodsQuery: {},
			objList: {},
			optionActivity: {
				emptyBtn: false,
				submitBtn: false,
				column: [
					{
						label: '活动名称',
						prop: 'acTitle',
						disabled: true,
						placeholder:'',
						span: 8,
						row: true
					},
					{
						label: '活动说明',
						prop: 'acContent',
						formslot: true,
						row: true,
						span: 8,
						disabled: true
					},
					{
						label: '参团人数',
						prop: 'activityPerson',
						tip: '参团人数最少为2',
						row: true,
						placeholder:'',
						valueDefault:2,
						span: 8,
						rules: [{
							required: true,
							message: '请输入参团人数'
						}],
					},
					{
						prop: 'goods',
						span: 24,
						labelWidth:5,
						formslot: true
					},
					{
						prop: 'goodsStock',
						span: 24,
						labelWidth:5,
						formslot: true
					}
				]
			},
			objGoods: {},
			//店铺商品列表
			optionGoods: {
				header: false,
				selection: true,
				selectable: (row) => {
					if(row.activityStatus) {
						return false;
					}else {
						return true;
					}
				},
				menu: false,
				search: true,
				searchMenuSpan:8,
				border: true,
				align:'center',
				column: [
					{
						label: '商品图片',
						prop: 'goodsMainPhotoUrl',
						slot: true,
						width:150,
					},
					{
						label: '商品名称',
						prop: 'goodsName',
						align:'left',
						searchSpan: 8,
						search: true
					},
					// {
					// 	label: '指定分类',
					// 	prop: 'goodsClassId',
					// 	dicUrl: '/base/goodsClass/listManage',
					// 	dicQuery: {
					// 		classLevel:2
					// 	},
					// 	searchSpan: 8,
					// 	props:{
					// 		label:'className',
					// 		value:'id'
					// 	},
					// 	hide: true,
					// 	type: 'select',
					// 	search: true
					// },
					{
						label: '总库存',
						prop: 'stock',
						width:100,
						span: 24
					},
					{
						label: '商城售价',
						prop: 'showPrice',
						width:150,
						span: 24
					}
				]
			},
			//新增
			newOption: {
				// size: 'mini',
				submitText: '完成',
				submitBtn:true,
				emptyBtn: false,
				labelWidth: '140',
				column: [
					{
						label: '活动名称',
						prop: 'acTitle',
						tip: '活动名称不超过10个字符',
						span: 16,
						maxlength: 10,
						disabled: true,
						row: true,
						rules: [
							{
								required: true,
								message: '请输入活动名称',
								trigger: 'blur'
							}
						]
					},
					// {
					// 	label: '报名截止时间',
					// 	prop: 'acApplyEndTime',
					// 	type: 'datetime',
					// 	formslot: true,
					// 	span: 16,
					// 	row: true,
					// 	rules: [
					// 		{
					// 			required: true,
					// 			message: '请选择报名截止时间',
					// 			trigger: 'blur'
					// 		}
					// 	]
					// },
					{
						label: "活动时间范围",
						prop: "acTime",
						formslot: true,
						type: "datetime",
						disabled: true,
						row: true,
						span: 16,
						rules: [{
							required: true,
							message: "请选择活动时间范围",
							trigger: "blur"
						}],
					},
					{
						label: "活动类型",
						prop: "groupBuyType",
						span: 16,
						disabled: true,
						type: "radio",
						row: true,
						dicData: YES_OR_NO,
						valueDefault:1,
					},
					{
						label: "是否凑团",
						prop: "joinGroup",
						span: 16,
						type: "radio",
						disabled: true,
						row: true,
						dicData: YES_OR_NO_JOIN_GROUP,
						valueDefault:1,
						display:true
					},
					{
						label: "闭团时间",
						prop: "groupTime",
						tip: '单位小时',
						span: 16,
						maxlength: 4,
						disabled: true,
						row: true,
						display:true,
						rules: [{
							required: true,
							message: "请输入闭团时间",
							trigger: "blur"
						},{
							required: true,
							validator: validatNum,
							trigger: 'blur'
						}],
					},
					// {
					// 	label: 'pc活动横幅',
					// 	prop: 'acPcAccUrl',
					// 	tip: '最佳尺寸:1280px*500px',
					// 	formslot: true,
					// 	span: 22,
					// 	row: true,
					// 	rules: [
					// 		{
					// 			required: true,
					// 			message: '请选择pc活动横幅',
					// 			trigger: 'change'
					// 		}
					// 	]
					// },
					{
						label: 'H5端活动横幅',
						prop: 'acH5AccUrl',
						tip: '最佳比例:2:1',
						formslot: true,
						disabled: true,
						row: true,
						span: 22,
						rules: [
							{
								required: true,
								message: '请选择H5端活动横幅',
								trigger: 'change'
							}
						]
					},
					// {
					// 	label: "APP导航图标",
					// 	prop: "appPcAccUrl",
					// 	formslot: true,
					// 	tip: '最佳尺寸:1:1',
					// 	span: 16,
					// 	row: true,
					// 	rules: [{
					// 		required: true,
					// 		message: "请选择APP导航图标",
					// 	}],
					// },
					// {
					// 	label: "店铺首页显示",
					// 	prop: "displayInStoreIndex",
					// 	span: 16,
					// 	type: "radio",
					// 	row: true,
					// 	dicData: YES_OR_NO2,
					// 	valueDefault: 1,
					// },
					{
						label: '邮费类型',
						prop: 'acFreeShipping',
						span: 16,
						type: 'radio',
						dicData: VAILD,
						valueDefault:1,
						disabled: true,
						row: true,
						rules: [
							{
								required: true,
								message: '请选择邮费类型',
								trigger: 'blur'
							}
						],
					},
					{
						label: '活动说明',
						prop: 'acContent',
						tip: '活动说明不超过50个字符',
						type: 'textarea',
						minRows: 2,
						disabled: true,
						span: 16,
						maxlength: 50,
						// rules:[{
						// 	required:true,
						// 	message:'请输入活动说明',
						// 	trigger: 'blur'
						// }]
					}
				]
			},
			actGoodsLoading: true,
			actGoodsPage: {
				pageSize: 10,
				currentPage: 1,
				total: 0
			},
			actGoodsQuery: {},
			//参加活动的商品列表
			optionList: {
				size: 'mini',
				excelBtn: false,
				addBtn: false,
				delBtn: false,
				editBtn: false,
				columnBtn: false,
				border: true,
				align: 'center',
				rowKey: 'id',
				height: 'auto',
				// calcHeight: 380,
				menuWidth:100,
				column: [
					{
						label: '商品名称',
						align:'left',
						prop: 'goodsName',
					},
					{
						label: '状态',
						prop: 'agStatus',
						type: 'select',
						width:100,
						dicData: Dict.STORE_BRAND_STATUS
					},
					{
						label: '活动名称',
						width:150,
						prop: 'acTitle'
					},
					{
						label: '开始时间',
						width:150,
						prop: 'agBeginTime'
					},
					{
						label: '结束时间',
						width:150,
						prop: 'agEndTime'
					},
				]
			},
			goodsStockList: [],
			objGoodsStock: {},
			goodsStock: {
				size: 'mini',
				excelBtn: false,
				addBtn: false,
				delBtn: false,
				editBtn: false,
				columnBtn: false,
				border: true,
				header: false,
				align: 'center',
				rowKey: 'id',
				menu: false,
				column: [
					{
						label: '商品名称',
						prop: 'goodsName',
						align:'left',
					},
					{
						label: '规格属性',
						width:150,
						prop: 'skuName'
					},
					{
						label: '库存数量',
						prop: 'stocks',
						width:120,
						slot: true
					},
					{
						label: '商城售价',
						prop: 'showPrice',
						width:120,
					},
					{
						label: '活动价格',
						prop: 'groupPrice',
						width:120,
						slot: true
					},
					{
						label: '每人限购次数',
						prop: 'maxTimes',
						width:120,
						slot: true
					},
					{
						label: '每人限购数量',
						prop: 'maxCount',
						width:120,
						slot: true
					},
					{
						label: '团长奖励金额',
						prop: 'leaderReward',
						width:120,
						slot: true
					}
				]
			},
			goodsStockSelf: {
				size: 'mini',
				excelBtn: false,
				addBtn: false,
				delBtn: false,
				editBtn: false,
				columnBtn: false,
				border: true,
				header: false,
				align: 'center',
				rowKey: 'id',
				menu: false,
				column: [{
					label: '商品名称',
					prop: 'goodsName',
					align:'left',
				},
					{
						label: '规格属性',
						width:150,
						prop: 'skuName'
					},
					{
						label: '库存数量',
						prop: 'stocks',
						width:120,
						slot: true
					},
					{
						label: '商城售价',
						prop: 'showPrice',
						width:120,
					},
					{
						label: '活动价格',
						prop: 'groupPrice',
						width:120,
						slot: true
					},
					{
						label: '每人限购次数',
						prop: 'maxTimes',
						width:120,
						slot: true
					},
					{
						label: '每人限购数量',
						prop: 'maxCount',
						width:120,
						slot: true
					}
				]
			},
			//所有活动
			option: {
				size: 'mini',
				excelBtn: false,
				addBtn: false,
				delBtn: false,
				editBtn: false,
				viewBtn: false,
				border: true,
				align: 'center',
				search: true,
				rowKey: 'id',
				columnBtn: false,
				menuWidth:290,
				searchMenuSpan:6,
				column: [
					{
						label: '活动名称',
						prop: 'acTitle',
						align:'left',
						fixed:true,
						search: true
					},
					{
						label: "创建时间",
						prop: "addTime",
						width: 150,
					},{
						label: "创建人",
						prop: "createUserName",
						width: 150,
					},
					{
						label: '开始时间',
						prop: 'acBeginTime',
						width:170,
					},
					{
						label: '结束时间',
						prop: 'acEndTime',
						width:170,
					},
					{
						label: '是否开启',
						prop: 'acStatus',
						dicData: Dict.ACTIVITY_STATE_ENUM,
						type: 'select',
						width:120,
						search: true
					},
					{
						label: '活动状态',
						prop: 'acStage',
						type: 'select',
						dicData: Dict.ACTIVITY_STAGE_ENUM,
						width:120,
						search: true
					 },
					//{
					// 	label: "店铺首页显示",
					// 	prop: "displayInStoreIndex",
					// 	type: 'select',
					// 	width: 100,
					// 	dicData: YES_OR_NO2,
					// },
				]
			}
		};
	},
	computed: {
		...mapGetters(['permission', 'userInfo']),
		permissionList() {
			return {

			};
		}
	},
	components: {
		selFile,
		addGoods
	},
	created() {
		this.getThisStoreInfo();
		// 获取分类
		getClassTree().then((res) => {
			if (res.status == 200) {
				if (res.data.code == 200) {
					this.chooseProductUserOption = res.data.data;
				} else {
					this.$message.error(res.data.message);
				}
			}
		});
	},
	watch:{
		'obj.groupBuyType'(val){
			if(val == 1){
				this.newOption.column[3]['display'] = true
				this.newOption.column[4]['display'] = false
			}else {
				this.newOption.column[3]['display'] = false
				this.newOption.column[4]['display'] = true
			}
		}
	},
	methods: {
		handleUserChange(value) {
			let val = [...value]
			let idArr = val.pop()
			this.goodsClassLabel = value
			this.goodsClassId = idArr
		},
		//查询店铺类型：自营非自营
		getThisStoreInfo(){
			getStoreInfo().then(res=> {
				if (res.data.code == 200) {
					const data = res.data;
					this.storeType = data.data.storeType;
					console.log("aaa" + this.storeType)
				}
			})
		},
		// 查看商品详情
		goodsDetail(id) {
			//查看
			this.goodsId = id;
			this.isCheck = true;
			this.dialogTableVisible = true;
		},
		//统计活动
		statisticsBut() {},
		activityLink(row) {
			//查看活动链接
			this.pcLink = row.acPcAccUrl;
			this.h5Link = baseUrl+'pages/activity/collage?id='+row.id;
			this.dialogLink = true;
		},
		//新增或者编辑 确定
		submit(loading, done) {
			this.loading = true;
			this.obj.type = 1;
			this.obj.acType = 2;
			this.obj.acBeginTime = this.obj.acTime[0];
			this.obj.acEndTime = this.obj.acTime[1];
			let timestamp = (new Date()).getTime();
			let beginTimestamp = new Date(this.obj.acBeginTime).getTime();
			let endTimestamp = new Date(this.obj.acEndTime).getTime();
			if(parseInt(timestamp)>parseInt(beginTimestamp) || parseInt(timestamp)>parseInt(endTimestamp)){
				this.onLoad(this.page);
				done();
				return this.$message.error("活动开始时间或者活动结束时间都不能小于当前时间");
			}
			saveOrUpdate(this.obj).then(
				res => {
					if (res.data.code == 200) {

						this.$message.success('操作成功!');
					} else {
						this.$message.error(res.data.message);
					}
					this.loading = false;
					this.closeDrawer();
					this.onLoad(this.page);
					done();
				},
				error => {
					done();
					console.log(error);
				}
			);
		},
		//新增编辑弹框的取消
		closeDrawer() {
			this.drawerNew = false;
			this.isView = false;
			this.$refs.form.resetForm();
			this.$nextTick(()=>{
				this.$refs["form"].clearValidate();
			});
		},
		//打开新增弹框
		addNew() {
			this.obj={};//清空表单的数据；
			this.obj.acStatus=1
			this.obj.acFreeShipping = 0;//默认不包邮
			this.deawerNewTitle = '新增活动';
			this.drawerNew = true;
			this.newOption.submitBtn=true;
			this.newOption.column[0].disabled=false;
			this.newOption.column[1].disabled=false;
			this.newOption.column[2].disabled=false;
			this.newOption.column[3].disabled=false;
			this.newOption.column[4].disabled=false;
			this.newOption.column[5].disabled=false;
			this.newOption.column[6].disabled=false;
			this.newOption.column[7].disabled=false;
		},
		//打开编辑弹框
		editAct(row) {
			this.deawerNewTitle = '编辑活动';
			this.obj = row;
			this.obj.acTime = []
			this.obj.acTime[0] = row.acBeginTime
			this.obj.acTime[1] = row.acEndTime
			console.log(this.obj)
			this.drawerNew = true;
			this.newOption.submitBtn=true;
			this.newOption.column[0].disabled=false;
			this.newOption.column[1].disabled=false;
			this.newOption.column[2].disabled=false;
			this.newOption.column[3].disabled=false;
			this.newOption.column[4].disabled=false;
			this.newOption.column[5].disabled=false;
			this.newOption.column[6].disabled=false;
			this.newOption.column[7].disabled=false;
		},
		//查看
		showAct(row) {
			console.log('row',row)
			this.obj=row;//清空表单的数据；
			this.obj.acTime=[row.acBeginTime,row.acEndTime]
			this.obj.acStatus=1
			this.obj.acFreeShipping = 0;//默认不包邮
			this.deawerNewTitle = '查看活动';
			this.drawerNew = true;
			this.isView = true
			this.newOption.submitBtn=false;
			this.newOption.column[0].disabled=true;
			this.newOption.column[1].disabled=true;
			this.newOption.column[2].disabled=true;
			this.newOption.column[3].disabled=true;
			this.newOption.column[4].disabled=true;
			this.newOption.column[5].disabled=true;
			this.newOption.column[6].disabled=true;
			this.newOption.column[7].disabled=true;
		},
		// cancel() {
		// 	this.$confirm("确定取消该活动?", {
		// 			confirmButtonText: "确定",
		// 			cancelButtonText: "取消",
		// 			type: "warning"
		// 		})
		// 		.then(() => {
		// 			console.log('取消活动')
		// 		})
		// 		.then(() => {
		// 			// this.onLoad(this.page);
		// 			this.$message({
		// 				type: 'success',
		// 				message: '操作成功！'
		// 			})
		// 		})
		// },
		//图片返回数据
		selFileCallBack(obj) {
			if (this.imageType == 1) {
				this.obj.acPcAccUrl = obj[0];
			} else if (this.imageType == 2) {
				this.obj.acH5AccUrl = obj[0];
			} else if (this.imageType == 3) {
				this.obj.appPcAccUrl = obj[0];
			}
			console.log(obj);
		},
		//打开选取图片的弹框
		uploadbtn(type) {
			if(!this.isView){
				this.imageType = type;
				this.$refs.selFile.open();
			}
		},

		//活动商品列表
		applyList(id) {
			this.actGoodsQuery.acId = id;
			this.onActGoodsLoad(this.actGoodsPage);
			this.drawerList = true;
		},
		//打开参与活动弹框
		enterActivity(row) {
			this.goodsQuery = {
				storeId: row.storeId,
				goodsStatus: 0,
				activityId: row.id
			};
			// var acTime = [];
			// row.acSessions.forEach(function(item) {
			// 	var data = {
			// 		label: '开始时间' + item.beginTime + '--结束时间' + item.endTime,
			// 		id: item.id
			// 	};
			// 	acTime.push(data);
			// });
			// console.log(acTime);
			// this.spikeTimes = acTime;
			this.onGoodsLoad(this.goodsPage);
			this.objActivity.actId = row.id;
			this.objActivity.acTitle = row.acTitle;
			this.objActivity.acContent = row.acContent;
			this.objActivity.acType = row.acType
			this.objActivity.groupBuyType = row.groupBuyType
			this.drawerActivity = true;
		},
		//参加活动时，选择商品的回调
		selectionChange(list) {
			this.idList = [];
			console.log(list);
			if (list.length > 0) {
				list.forEach((item, index) => {
					this.idList[index] = item.id;
				});
				showGoodsStock(this.idList).then(res => {
					if (res.data.code == 200) {
						this.goodsStockList = res.data.data;
						this.goodsStockList.forEach(item=>{
							item.skuPrice = item.groupPrice
						})
					} else {
						this.$message.error(res.data.message);
					}
				});
			} else {
				this.idList = [];
				this.goodsStockList = [];
			}

			console.log(this.idList);
		},
		//确认参与活动
		handleSubmit() {
			this.load = true;
			if (!this.objActivity.activityPerson) {
				this.load = false;
				this.$message.error('请输入参团人数');
				return;
			} 
			var re = /^[0-9]+[0-9]*[0-9]*$/ ;
			if(!re.test(this.objActivity.activityPerson) ){
				this.$message.error('参团人数必须为正整数')
				this.load=false
				return;
			}else if(this.objActivity.activityPerson<2){
				this.$message.error('参团人数最少为2')
				this.load=false
				return;
			}else if(this.objActivity.activityPerson>10){
				this.$message.error('参团人数最大为10')
				this.load=false
				return;
			}else if (this.idList.length == 0) {
				this.load = false;
				this.$message.error('至少选择一个商品参加活动');
				return;
			}
			var rep= /^(0|[1-9][0-9]{0,9})(\.[0-9]{1,2})?$/;
			for (let item of this.goodsStockList) {
				item.activityPerson = this.objActivity.activityPerson;
				// 判断库存量
				if(!this.testIsInteger(item.stocks)){
					this.$message.error('库存数量应为大于0的整数');
					this.load=false
					return;
				}else if(Number(item.stocks)>1000000 ){
					this.$message.error('库存数量不能大于1000000');
					this.load=false
					return;
				}
				//判断输入的价格是否是大于等于0的整数或者小数
				if(!rep.test(item.groupPrice)){
					if(Number(item.groupPrice)>1000000 ){
						this.$message.error('价格不能大于1000000');
					}else{
						this.$message.error('价格应为大于0的数（保留两位小数）');
					}
					this.load=false
					return;
				}else if(Number(item.groupPrice)>1000000 ){
					this.$message.error('价格不能大于1000000');
					this.load=false
					return;
				}
				// 判断限购次数
				if(!this.testIsInteger(item.maxTimes)){
					this.$message.error('限购次数格应为大于0的整数');
					this.load=false
					return;
				}else if(Number(item.maxTimes)>1000000 ){
					this.$message.error('限购次数不能大于1000000');
					this.load=false
					return;
				}
				// 判断限购数量
				if(!this.testIsInteger(item.maxCount)){
					this.$message.error('限购数量应为大于0的整数');
					this.load=false
					return;
				}else if(Number(item.maxCount)>1000000 ){
					this.$message.error('限购数量不能大于1000000');
					this.load=false
					return;
				}
				// 判断团长金额
				if(item.leaderReward) {
					if(!validatPrice(item.leaderReward)) {
						this.$message.error('团长可得金额为大于等于0的值(可精确到0.01)');
						this.load=false
						return
					}
				}
			}
			var data = {
				actId: this.objActivity.actId,
				activityPerson: this.objActivity.activityPerson,
				goodsIds: this.idList,
				acGoodsSkuList: encodeURI(JSON.stringify(this.goodsStockList))
			};
			joinActivity(data).then(res => {
				if (res.data.code == 200) {
					this.$message({
						type: 'success',
						message: '操作成功！'
					});
					this.handleEmpty()
				} else {
					this.$message.error(res.data.message);
					//this.drawerActivity = false;
					this.$refs.goodsCrud.toggleSelection();
					this.goodsStockList = [];
				}
				this.load = false;
			});
			console.log(data);
		},
		//校验是否是大于等于0的整数
		testIsInteger(val){
			let flag = false;
			var re = /^[0-9]*[1-9][0-9]*$/; //是否是大于或者等于0的整数
			if(re.test(val)){
				flag = true;
			}
			else {
				flag = false;
			}
			return flag;
		},
		//关闭参与活动
		handleEmpty() {
			this.drawerActivity = false;
			this.$refs.goodsCrud.toggleSelection();
			this.$refs.goodsCrud.searchReset();
			this.goodsStockList = [];
		},
		// 查看参加活动的商品时的商品表格 清空搜索
		searchActGoodsReset() {
			this.actGoodsQuery = {};
			this.onActGoodsLoad(this.actGoodsPage);
		},
		searchActGoodsChange(params) {
			this.actGoodsQuery = params;
			this.onActGoodsLoad(this.actGoodsPage, params);
		},

		currentActGoodsChange(currentPage) {
			this.actGoodsPage.currentPage = currentPage;
			this.onActGoodsLoad(this.actGoodsPage);
		},
		sizeActGoodsChange(pageSize) {
			this.actGoodsPage.pageSize = pageSize;
			this.onActGoodsLoad(this.actGoodsPage);
		},
		refreshActGoodsChange() {
			this.currentActGoodsChange(1);
			this.onActGoodsLoad(this.actGoodsPage);
		},
		onActGoodsLoad(actGoodsPage, params = {}) {
			console.log(actGoodsPage);
			console.log(params);
			listActivity(actGoodsPage.currentPage, actGoodsPage.pageSize, Object.assign(params, this.actGoodsQuery)).then(res => {
				if (res.data.code == 200) {
					this.actGoodsLoading = false;
					const data = res.data;
					this.actGoodsPage.total = data.count;
					for (let item of data.data) {
						item.agBeginTime = dateFormat(new Date(item.agBeginTime));
						item.agEndTime = dateFormat(new Date(item.agEndTime));
					}
					console.log(data);
					this.dataList = data.data;
				} else {
					this.$message.error(res.data.message);
				}
			});
		},
		searchGoodsReset() {
			this.idList = [];
			this.goodsClassId=''
			this.goodsClassLabel = ''
			this.goodsQuery.goodsClassId=''
			this.$refs.goodsCrud.toggleSelection();
			this.onGoodsLoad(this.goodsPage);
		},
		searchGoodsChange(params,done) {
			this.idList = [];
			this.$refs.goodsCrud.toggleSelection();
			this.goodsPage.currentPage = 1;
			params.goodsStatus=0
			params.goodsClassId=this.goodsClassId
			let activityId = this.goodsQuery.activityId;
			params.activityId = activityId;
			this.goodsQuery = params;
			this.onGoodsLoad(this.goodsPage, params,done);
		},

		currentGoodsChange(currentPage) {
			this.goodsPage.currentPage = currentPage;
			this.onGoodsLoad(this.goodsPage);
		},
		sizeGoodsChange(pageSize) {
			this.goodsPage.pageSize = pageSize;
			this.onGoodsLoad(this.goodsPage);
		},
		refreshGoodsChange() {
			this.idList = [];
			this.$refs.goodsCrud.toggleSelection();
			this.onGoodsLoad(this.goodsPage);
		},
		onGoodsLoad(goodsPage, params = {},done) {
			this.goodsloading = true;
			params.goodsStatus = 0;
			getGoodsList(goodsPage.currentPage, goodsPage.pageSize, Object.assign(params, this.goodsQuery)).then(res => {
				if(done){
					done();
				}
				this.goodsloading = false;
				const data = res.data;
				this.goodsPage.total = data.count;
				this.goodsList = data.data;
			});
		},

		searchReset() {
			this.acTime=''
			this.query = {
				acType:2,
				type:1
			};
			this.onLoad(this.page);
		},
		searchChange(params) {
			this.page.currentPage=1
			params.acBeginTime = this.acTime[0]
			params.acEndTime = this.acTime[1]
			params.acType=2
			params.type=1
			console.log(params)
			this.query = params;
			this.onLoad(this.page, params);
		},

		currentChange(currentPage) {
			this.page.currentPage = currentPage;
			this.onLoad(this.page);
		},
		sizeChange(pageSize) {
			this.page.pageSize = pageSize;
			this.onLoad(this.page);
		},
		refreshChange() {
			this.onLoad(this.page);
		},
		onLoad(page, params = {}) {
			this.loading = true;
			this.loading = false;
			getActList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
				const data = res.data;
				this.page.total = data.count;
				this.data = data.data;
			});
		}
	}
};
</script>

<style scoped>
.drawerHeader {
	font-size: 20px;
	padding: 30px 20px;
}

.upload {
	display: flex;
	justify-content: flex-start;
	align-items: center;
}

.tsmsg {
	width: 300px;
	font-size: 10px;
	color: #999;
	margin: 0px;
	padding-left: 10px;
}

.diaLink {
	font-size: 20px;
	padding: 10px;
}

.diaLink p {
	padding: 10px;
}
</style>
